package com.bawei.mvvm.db

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import com.bawei.mvvm.App
import com.bawei.mvvm.entity.BannerData
import com.youth.banner.Banner

@Database(entities = [BannerData::class], version = 2, exportSchema = true)
abstract class AppDataBase:RoomDatabase(){
   abstract fun  getBannerDao():BannerDao
   //伴生对象：静态
   companion object{
       private  var appDataBase:AppDataBase? = null
       fun getInstance():AppDataBase?{
           if(appDataBase == null){
               synchronized(AppDataBase::class){
                   if(appDataBase == null){
                       appDataBase = Room.databaseBuilder(App.instance,AppDataBase::class.java,"mvvm.db")
                           .allowMainThreadQueries()//数据库操作默认是耗时操作，允许主线程执行
                           .addMigrations(Migration1_2)
                           .build()

                   }
               }
           }
           return appDataBase
       }
       private val Migration1_2:Migration = Migration(1,2){
           it.execSQL("CREATE table goods (id INTEGER PRIMARY KEY,goods_desc TEXT)")
       }




   }


}